<?php

namespace app\adminmall\controller;
use app\adminmall\model\SiteModel;
use app\common\controller\AdminBaseController;
use think\Db;
/**
 * 商城设置
 */
class SiteController extends AdminBaseController
{
 	 protected $model;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new SiteModel();
    }
    /**
     * 商城基本信息设置
     * @author [lx] 
     */
    public function index(){
        $site=$this->model->where("key","copyright")->find();
        if(request()->isAjax()){
        	$param=input('post.');
			$return =$this->model->setting_save($param, "copyright","base_info");
			if ($return) {
                    $this->success('保存成功', url('Site/index'));
            } else {
                $this->error('保存失败');
            }
        }
        return $this->fetch('index', ['settings' => iunserializer($site['value'])]);
    }
	 /**
     * 商城分销设置
     * @author [lx] 
     */
    public function distribution(){
        $site=$this->model->where("key","distribution")->find();
        if(request()->isAjax()){
        	$param=input('post.');
			$return =$this->model->setting_save($param, "distribution","distribution");
			if ($return) {
				if (cache('setting')) {
					$setting=cache('setting');
				}
				$setting['distribution']=$this->model->where("key","distribution")->find();
				cache("setting",$setting);
             	 $this->success('保存成功', url('Site/distribution'));
            } else {
                $this->error('保存失败');
            }
        }
        return $this->fetch('distribution', ['settings' => iunserializer($site['value'])]);
    }
	/**
	 * 物流列表
	 */
	 public function logistics( $page = 1){
        $list=$this->model->where("inc_type","logistics")->select();
		foreach($list as $s=>$v){
			$list[$s]['value']=iunserializer($list[$s]['value']);
		}
        return $this->fetch('logistics', ['list' =>$list] );
    }

    /**
     * 
     *添加物流方式
     * @author [lx] 
     */
    public function add_logistics()
    {
        if(request()->isAjax()){
        	$param=input('post.');
			$return =$this->model->setting_save($param,$param['no'],"logistics");
			if ($return) {
                    $this->success('保存成功', url('Site/logistics'));
            } else {
                $this->error('保存失败');
            }
        }
        return $this->fetch();
    }


    /**
     * [edit 编辑]
     * @return [type] [description]
     * @author [lx]
     */
    public function edit_logistics($id)
    {
        if(request()->isAjax()){
            $param = input('post.'); 
			$data['value']['name']=$param['name'];
			$data['value']['no']=$param['no'];
			$data['value']['thumb']=$param['thumb'];
			$data['key']=$param['no'];
			$data['value']=iserializer($data['value']);
         	if ($this->model->where("id",$id)->update($data)!== false) {
                    $this->success('修改成功', url('Site/logistics'));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
		$model=$this->model->find($id);
		$model['value']=iunserializer($model['value']);
        $this->assign('model',$model);
        return $this->fetch();
    }



    /**
     * [del删除]
     * @author [lx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }
}